using System;
using System.Collections.Generic;
using System.Text;

using SpeechLib;

namespace Audible
{
    public delegate void SpeechStopEvent();
    class MediaPlayer
    {
        private System.Media.SoundPlayer soundPlayer;
        private SpVoiceClass voice;
        public event SpeechStopEvent OnSpeechStop;
        public int VoiceSpeechSpeed
        {
            get { return voice.Rate; }
            set { voice.Rate = value; }
        }

        public MediaPlayer()
        {
            // Create the sound player
            soundPlayer = new System.Media.SoundPlayer();
            // Create the text to speech voice
            voice = new SpVoiceClass();
            voice.EndStream += new _ISpeechVoiceEvents_EndStreamEventHandler(voice_EndStream);
        }

        public void PlayTick()
        {
            // Play a simple 'tick' sound
            soundPlayer.Stream = Properties.Resources.tick;
            soundPlayer.Play();
        }

        public void PlaySpeechSpeed(SpeechSpeed speechSpeed)
        {
            switch (speechSpeed)
            {
                case SpeechSpeed.Normal:
                    soundPlayer.Stream = Properties.Resources.chimes;
                    break;
                case SpeechSpeed.Slow:
                    soundPlayer.Stream = Properties.Resources.chimes_slow;
                    break;
                case SpeechSpeed.Fast:
                    soundPlayer.Stream = Properties.Resources.chimes_fast;
                    break;
            }
            soundPlayer.Play();
        }

        public void PlayNavMode(NavMode navMode)
        {
            switch (navMode)
            {
                case NavMode.Book:
                    soundPlayer.Stream = Properties.Resources.book;
                    break;
                case NavMode.Chapter:
                    soundPlayer.Stream = Properties.Resources.chapter;
                    break;
                case NavMode.Verse:
                    soundPlayer.Stream = Properties.Resources.verse;
                    break;
            }
            soundPlayer.Play();
        }

        public void PlayFile(string file)
        {
            string soundLocation = @"wavs\" + file;
            if (System.IO.File.Exists(soundLocation))
            {
                soundPlayer.SoundLocation = soundLocation;
                soundPlayer.Play();
            }
        }

        public void Speak(string text)
        {
            StopSpeech();
            voice.Speak(text, SpeechVoiceSpeakFlags.SVSFlagsAsync);
        }

        public void StopSpeech()
        {
            voice.Speak(null, SpeechVoiceSpeakFlags.SVSFPurgeBeforeSpeak);
        }

        private void voice_EndStream(int StreamNumber, object StreamPosition)
        {
            if (OnSpeechStop != null)
                OnSpeechStop();
        }
    }
}
